// TODO: CODE REVIEW & CLEANUP!
//
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

using HomeUX.Controls;

namespace HomeUX.ClientAppSupport
{

public partial class ConnectionSettingsScreen : UserControl
{
    Screen _screen;

    Brush _normalTextForeground;
    Brush _errorTextForeground;

    public ConnectionSettingsScreen()
    {
        InitializeComponent();
        Loaded += ConnectionSettingsScreen_Loaded;
        KeyDown += ConnectionSettingsScreen_KeyDown;
        SavePasswordButton.Click += SavePasswordButton_Click;
        _normalTextForeground = ServerNameTextBox.Foreground;
        _errorTextForeground = (Brush)Resources["ErrorTextForegroundBrush"];
    }

    void ConnectionSettingsScreen_Loaded(object sender, RoutedEventArgs e)
    {
        _screen = ClientFramework.Current.TryFindScreen(this);
        _screen.Opened += _screen_Opened;
        _screen.Closed += _screen_Closed;
        _screen.Refreshed += _screen_Refreshed;
    }

    void _screen_Opened(object sender, EventArgs e)
    {
        ServerNameTextBox.Text = ClientFramework.Current.ServerHostName;
        ServerPortTextBox.Text = ClientFramework.Current.ServerPortNumber.ToString();
        PasswordTextBox.Text = ClientFramework.Current.ServerPassword;
        PasswordTextBox.Foreground = _normalTextForeground;
        SavePasswordButton.IsLit = ClientFramework.Current.IsPasswordSaved;
        UpdatePasswordWarning();
        UpdateSimulationUi();

        if (!ClientFramework.Current.IsSimulatedServerMode)
        {
            ServerNameTextBox.TextBox.SelectAll();
            ServerPortTextBox.TextBox.SelectAll();
            PasswordTextBox.TextBox.SelectAll();
        }

        switch (ClientFramework.Current.ConnectionStatus)
        {

        case ConnectionStatus.ConnectionSettingsNeeded:

            IntroText.Foreground = _errorTextForeground;
            if ((ServerNameTextBox.Text.Length > 0) && (ServerPortTextBox.Text.Length > 0))
            {
                // only the password is needed
                IntroText.Text = "Please enter the password";
                PasswordTextBox.Foreground = _errorTextForeground;
                PasswordTextBox.TextBox.SelectAll();
                Dispatcher.BeginInvoke(() => PasswordTextBox.Focus());
            }
            else
            {
                // more than just the password is needed
                IntroText.Text = "Please enter server information";
            }
            break;

        case ConnectionStatus.PasswordIsIncorrect:

            IntroText.Foreground = _errorTextForeground;
            IntroText.Text = "The password is incorrect";
            PasswordTextBox.Foreground = _errorTextForeground;
            PasswordTextBox.TextBox.SelectAll();
            Dispatcher.BeginInvoke(() => PasswordTextBox.Focus());
            break;

        default:

            IntroText.Foreground = _normalTextForeground;
            IntroText.Text = "Connection settings";
            break;

        }
    }

    void _screen_Closed(object sender, EventArgs e)
    {
        if (ClientFramework.Current.ConnectionStatus != ConnectionStatus.Connected)
            ClientFramework.Current.BeginConnectingToServer();
    }

    void _screen_Refreshed(object sender, EventArgs e)
    {
        _screen_Opened(sender, e);
    }

    void ConnectionSettingsScreen_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            e.Handled = true;
            ConnectToServerButton_Click(null, null);
        }
    }

    void SavePasswordButton_Click(object sender, RoutedEventArgs e)
    {
        SavePasswordButton.IsLit = !SavePasswordButton.IsLit;
        UpdatePasswordWarning();
        ClientFramework.Current.SaveConnectionSettings(SavePasswordButton.IsLit);
    }

    void UpdatePasswordWarning()
    {
        PasswordWarning.Visibility = SavePasswordButton.IsLit ? Visibility.Visible : Visibility.Collapsed;
    }

    void UpdateSimulationUi()
    {
        bool simulated = ClientFramework.Current.IsSimulatedServerMode;
        SimulatedServerModeButton.IsLit = simulated;
        ServerNameTextBox.IsEnabled = !simulated;
        ServerPortTextBox.IsEnabled = !simulated;
        PasswordTextBox.IsEnabled = !simulated;
        ConnectToServerButton.IsEnabled = !simulated;
    }

    private void ConnectToServerButton_Click(object sender, RoutedEventArgs e)
    {
        // can't be connected when server settings change
        ClientFramework.Current.DisconnectFromServer();

        int portNumber;
        if (!int.TryParse(ServerPortTextBox.Text, out portNumber))
        {
            ServerPortTextBox.TextBox.SelectAll();
            ServerPortTextBox.Focus();
            TouchMessageBox.Show(this, "Not a valid number", "OK", null);
            return;
        }

        ClientFramework.Current.ServerHostName = ServerNameTextBox.Text;
        ClientFramework.Current.ServerPortNumber = portNumber;
        ClientFramework.Current.ServerPassword = PasswordTextBox.Text;
        ClientFramework.Current.SaveConnectionSettings(SavePasswordButton.IsLit);
        ClientFramework.Current.BeginConnectingToServer();
    }

    private void SimulatedServerModeButton_Click(object sender, RoutedEventArgs e)
    {
        ClientFramework.Current.DisconnectFromServer();
        ClientFramework.Current.IsSimulatedServerMode = !ClientFramework.Current.IsSimulatedServerMode;
        ClientFramework.Current.SaveConnectionSettings(SavePasswordButton.IsLit);
        if (ClientFramework.Current.IsSimulatedServerMode)
            ClientFramework.Current.BeginConnectingToServer();
        UpdateSimulationUi();
    }
}

}
